import { ApiResponse } from '../types';
import {
  register as registerService,
  login as loginService,
  logout as logoutService,
  RegisterRequest,
  RegisterResponse,
  LoginRequest,
  LoginResponse
} from '../service/authService';

/**
 * Mock API: 用户注册
 * POST /api/auth/register
 */
export const POST_register = async (body: RegisterRequest): Promise<ApiResponse<RegisterResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 调用Service层
    const result = await registerService(body);

    return result;
  } catch (error) {
    console.error('Register API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 用户登录
 * POST /api/auth/login
 */
export const POST_login = async (body: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await loginService(body);

    return result;
  } catch (error) {
    console.error('Login API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 用户登出
 * POST /api/auth/logout
 */
export const POST_logout = async (): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 200));

    // 调用Service层
    const result = await logoutService();

    return result;
  } catch (error) {
    console.error('Logout API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};